﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpModifyCmd.h"
#include "GbmpUiPlatformCommandIds.h"
#include "GbmpUiPlatformCommandRegister.h"
#include "CmdStatusUtils.h"
#include "ActionModifyInput.h"
#include "GbmpModifyElementsBehavior.h"
#include "GmPickProfileCurveFirst.h"
#include "GbmpPickLinkDocumentElementBeforeProxy.h"
#include "ActionModifyCreator.h"
#include "GbmpModifyCmdUtils.h"
#include "GmPickGroupEventHandle.h"
#include "GmRectPickGroupEventHandler.h"
#include "PickSuperInstanceBeforeSubInstanceEventHandler.h"
#include "RectPickSuperInstanceBeforeSubInstanceEventHandler.h"

// 近程捕捉、远程捕捉建议添加接口

#include "GbmpActionModify.h"

#include "GbmpActionModify.h"
#include "IEditMode.h"
#include "UiDocumentViewUtils.h"

#include "ISelection.h"

//公共组件
#ifdef UiAdapter_IMPL
#include "UiAdapter.h"
#endif
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"

using namespace gcmp;

REGISTER_GM_COMMAND(GbmpModifyCmd)
REGISTER_GM_COMMAND(GbmpModifyFilterFaceCmd)

// GbmpModifyCmd
GbmpModifyCmd::GbmpModifyCmd()
    : GbmpCommandBase(ID_CMD_GBMP_MODIFY)
{

}

GbmpModifyCmd::~GbmpModifyCmd()
{
}

bool GbmpModifyCmd::IsEnabled() const
{
    if (!CommandBase::IsEnabled())
        return false;

    CMD_ONLY_ENABLE_IN_DOCUMENT_ENVIRONMENT;
}

bool GbmpModifyCmd::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_DOCUMENT_ENVIRONMENT;
}

OwnerPtr<IAction> GbmpModifyCmd::ExecuteCommand(const CommandParameters& cmdParams)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    //TODO:最终采用如下这段
    //ActionModifyInput actionInput(true, false, true);
    //actionInput.SetSelectByFaceInteriorEnabled(true);
    //actionInput.SetActionModifyBehavior(NEW_AS_OWNER_PTR(GbmpModifyElementsBehavior));
    //actionInput.AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GmPickProfileCurveFirstEventHandler));
    //opAction = ActionModifyCreator::Create(actionInput);

    //////////////////////////////////////////////////////////////////////////
    //TODO：以下部分Api改造完善后请清除
    // 注意： 请及时清理！
    OwnerPtr<IAction> opAction;

#ifdef UiAdapter_IMPL
    ActionModifyInput actionInput(true, false, false);
    Sample::UiAdapter::ApplyPickPostProcessEventHandler(actionInput);
#endif
    IEditMode *pEditMode = IEditMode::GetTopActiveEditMode(pDoc);
    if (CmdStatusUtils::IsInAnimationEnvironment() ||(pEditMode && pEditMode->GetEditModeType() == L"PropertyEditModeTypes"))
    {
        OwnerPtr<GbmpActionModify> opActionModify = NEW_AS_OWNER_PTR(GbmpActionModify);
        opActionModify->SetModifyElementsBehavior(NEW_AS_OWNER_PTR(GbmpModifyElementsBehavior));
        opActionModify->AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GmPickProfileCurveFirstEventHandler));
        opActionModify->SetIsAllowedMove(true);
        opActionModify->SetIsPickingHighlightOnlyGraphicsNodeAllowed(false);
        opActionModify->SetSelectByFaceInteriorEnabled(true);
#ifdef UiAdapter_IMPL
        for (auto& handler : actionInput.GetPickPostProcessEventHandler())
        {
            opActionModify->AddPickPostProcessEventHandler(TransferOwnership(handler));
        }
#endif
        opAction = TransferOwnership(opActionModify);
    }
    else
    {
        //TODO:测试示例ActionModify通用编辑的Api实现Gbmp版本的ActionModify
        OwnerPtr<GbmpActionModify> opActionModify = NEW_AS_OWNER_PTR(GbmpActionModify);
        opActionModify->SetModifyElementsBehavior(NEW_AS_OWNER_PTR(GbmpModifyElementsBehavior));
        opActionModify->AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GmPickProfileCurveFirstEventHandler));
        if (GbmpPickProxyBeforeLinkDocumentEventHandler::DocumentHasLinkedEditableDocuments(pDoc))
        {
            opActionModify->AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GbmpPickProxyBeforeLinkDocumentEventHandler));
        }
        else
        {
            opActionModify->AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GbmpPickLinkDocumentBeforeProxyEventHandler));
        }
        opActionModify->AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GmPickGroupEventHandler));
        opActionModify->AddRectPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GmRectPickGroupEventHandler));
        opActionModify->AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(PickSuperInstanceBeforeSubInstanceEventHandler));
        opActionModify->AddRectPickPostProcessEventHandler(NEW_AS_OWNER_PTR(RectPickSuperInstanceBeforeSubInstanceEventHandler));
        opActionModify->SetIsAllowedMove(true);
        opActionModify->SetIsPickingHighlightOnlyGraphicsNodeAllowed(false);
        opActionModify->SetSelectByFaceInteriorEnabled(true);
#ifdef UiAdapter_IMPL
        for (auto& handler : actionInput.GetPickPostProcessEventHandler())
        {
            opActionModify->AddPickPostProcessEventHandler(TransferOwnership(handler));
        }
#endif
        opAction = TransferOwnership(opActionModify);
    }
    // 注意： 请及时清理！
    //////////////////////////////////////////////////////////////////////////

   
    GbmpModifyCmdUtils::SetUiViewDefaultActionCommand(ID_CMD_GBMP_MODIFY);

    return TransferOwnership(opAction);
}


// GbmpModifyFilterFaceCmd
GbmpModifyFilterFaceCmd::GbmpModifyFilterFaceCmd()
    : GbmpCommandBase(ID_CMD_GBMP_MODIFY_FILTER_FACE)
{
}

GbmpModifyFilterFaceCmd::~GbmpModifyFilterFaceCmd()
{
}

bool GbmpModifyFilterFaceCmd::IsEnabled() const
{
    if (!CommandBase::IsEnabled())
        return false;

    CMD_ONLY_ENABLE_IN_DOCUMENT_ENVIRONMENT;
}

bool GbmpModifyFilterFaceCmd::IsVisible() const
{
    CMD_ONLY_VISIBLE_IN_DOCUMENT_ENVIRONMENT;
}

OwnerPtr<IAction> GbmpModifyFilterFaceCmd::ExecuteCommand(const gcmp::CommandParameters& cmdParams)
{
    IDocument* pDoc = UiDocumentViewUtils::GetCurrentDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    ActionModifyInput actionInput(true, false, false);
    actionInput.SetSelectByFaceInteriorEnabled(false);
    actionInput.SetActionModifyBehavior(NEW_AS_OWNER_PTR(GbmpModifyElementsBehavior));
    actionInput.AddPickPostProcessEventHandler(NEW_AS_OWNER_PTR(GmPickProfileCurveFirstEventHandler));
#ifdef UiAdapter_IMPL
    Sample::UiAdapter::ApplyPickPostProcessEventHandler(actionInput);
#endif
    OwnerPtr<IAction> opAction = ActionModifyCreator::Create(actionInput);

    GbmpModifyCmdUtils::SetUiViewDefaultActionCommand(ID_CMD_GBMP_MODIFY_FILTER_FACE);

    return TransferOwnership(opAction);
}
